package com.example.newsdaily.slice;

import com.alibaba.fastjson.JSON;
import com.example.newsdaily.MyApplication;
import com.example.newsdaily.ResourceTable;
import com.example.newsdaily.adapter.CommonProvider;
import com.example.newsdaily.adapter.ViewHolder;
import com.example.newsdaily.entity.TCollection;
import com.example.newsdaily.utl.ContainUtil;
import com.example.newsdaily.utl.HttpClientUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.ListContainer;

import java.util.ArrayList;
import java.util.List;

public class CollectionAbilitySlice extends AbilitySlice {
    public static TCollection tCollection;
    static List<TCollection> collectionList = new ArrayList<>();
    public static ListContainer listContainer;
    public static CommonProvider<TCollection> commonProvider;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_collection);

        listContainer = (ListContainer) findComponentById(ResourceTable.Id_collection_news);
        update();


        listContainer.setItemSelectedListener((listContainer1, component, i, l) -> {
            tCollection = collectionList.get(i);
            listContainer.getItemProvider().notifyDataChanged();
        });


    }

    public void update() {
        new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        String result = HttpClientUtil.doGet(ContainUtil.FIND_COLLECTION_BY_USER+"?TUser="+ MyApplication.tuser.getId());
                        collectionList = JSON.parseArray(result, TCollection.class);
                        getUITaskDispatcher().asyncDispatch(new Runnable() {
                            @Override
                            public void run() {
                                commonProvider = new CommonProvider<TCollection>(collectionList, getContext(), ResourceTable.Layout_collection_news_item) {
                                    @Override
                                    protected void convert(ViewHolder holder, TCollection item, int position) {
                                        holder.setImageResource(ResourceTable.Id_collection_image, ContainUtil.detail_pic_id[item.gettBriefNews().getPicId()]);
                                        holder.setText(ResourceTable.Id_collection_title, item.gettBriefNews().getTitle());
                                        holder.setText(ResourceTable.Id_collection_content, item.gettBriefNews().getContent());
                                        holder.setText(ResourceTable.Id_collection_publisher, item.gettBriefNews().getPublisher());
                                        Button deleteButton = holder.getView(ResourceTable.Id_collection_delete_btn);

                                        holder.getView(ResourceTable.Id_collection_delete_btn).setClickedListener(component -> {
                                            collectionList.remove(item);
                                            listContainer.getItemProvider().notifyDataChanged();
                                            new Thread(
                                                    new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            HttpClientUtil.doGet(ContainUtil.DELETE_COLLECTION_NEW+"?id="+item.getId());
                                                        }
                                                    }
                                            ).start();
                                        });
                                    }
                                };
                                listContainer.setItemProvider(commonProvider);
                            }
                        });
                    }
                }
        ).start();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
